﻿#region Licence
// <copyright file="Helpers/HslColorHelper.cs" company="Kopigi">
// Copyright © Kopigi 2014
// </copyright>
// ****************************************************************************
// <author>Marc PLESSIS</author>
// <date>11/10/2014</date>
// <project>Nentang.wpf</project>
// <web>http://nentang.codeplex.com</web>
// <license>
// The MIT License (MIT)
// 
// Copyright (c) 2013 Marc Plessis
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// </license>
#endregion
#region using

using System;
using System.Windows.Media;

#endregion

namespace Helpers.HSL
{
    /// <summary>
    /// Helper de la classe HslColor
    /// </summary>
    public static class HslColorHelper
    {
        private const int SMax = 240;
        private const int LMax = 240;
        private const int HMax = 360;

        /// <summary>
        /// Permet de générer une nouvelle couleur en modifiant les composantes HSL (teinte, saturation, luminance)
        /// </summary>
        /// <param name="color">Couleur de départ</param>
        /// <param name="hueChanged">valeur de modification de la teinte</param>
        /// <param name="saturationChanged">valeur de modification de la saturation</param>
        /// <param name="luminosityChanged">valeur de modification de la luminance</param>
        /// <returns>Couleur générée</returns>
        public static Color ChangedHslValueInColor(Color color, double hueChanged, double saturationChanged, double luminosityChanged)
        {
            return ChangedHslValue(color, hueChanged, saturationChanged, luminosityChanged);
        }

        /// <summary>
        /// Permet de générer une nouvelle couleur en modifiant les composantes HSL (teinte, saturation, luminance).
        /// Les valeurs de modification de la saturation et de la luminance étatn indiquées en pourcentage
        /// </summary>
        /// <param name="color">Couleur de départ</param>
        /// <param name="hueChanged">valeur de modification de la teinte</param>
        /// <param name="saturationPercentChanged">valeur (en pourcentage) de modification de la saturation</param>
        /// <param name="luminosityPercentChanged">valeur (en pourcentage) de modification de la luminance</param>
        /// <returns>Couleur générée</returns>
        public static Color ChangedHslValueInColorPercent(Color color, double hueChanged, double saturationPercentChanged, double luminosityPercentChanged)
        {
            //On récupére les valeurs de composantes traduite du pourcentage voulu
            double saturationChanged = (saturationPercentChanged * SMax) / 100;
            double luminosityChanged = (luminosityPercentChanged * LMax) / 100;

            return ChangedHslValue(color, hueChanged, saturationChanged, luminosityChanged);
        }

        #region  private

        private static Color ChangedHslValue(Color color, double hueChanged, double saturationChanged, double luminosityChanged)
        {
            //On récupérer une couleur au format HSL
            var hslColor = new HslColor(color);

            //On créé une nouvelle couleur
            var sat = Convert.ToInt32(Math.Round((hslColor.Saturation + saturationChanged) > SMax ? SMax : (hslColor.Saturation + saturationChanged)));
            var lum = Convert.ToInt32(Math.Round((hslColor.Luminosity + luminosityChanged) > LMax ? LMax : (hslColor.Luminosity + luminosityChanged)));
            var hue = Convert.ToInt32(Math.Round((hslColor.Hue + hueChanged) > HMax ? HMax : (hslColor.Hue + hueChanged)));
            return new HslColor(hue, sat, lum).ToColorRgb();
        }

        #endregion
    }
}
